import {mapState,mapMutations} from 'vuex';

export default {
  name: "cities",
  data: () => ({
    cityList:[],
    num:0,
    cityIdx:0,
    scrollTop:0,
  }),
  computed:{
    //这里的三点叫做 : 扩展运算符
    ...mapState({
      openCity:state=>state.house.openCity,

    }),
  },
  methods: {
    ...mapMutations([
      'setopenCity',
      'setServicePhone',
      'setUserInfo'
    ]),
    chooseCity (index) {   //选择当前城市
      var that = this;
      // console.log(index);
      var cityList = that.cityList;
      for (var i=0; i<cityList.length; i++) {
        cityList[i].isChoose = false;
        if(i==index){
          cityList[i].isChoose = true;
          that.$emit('openCity',cityList[i]);
          that.setopenCity(cityList[i]);    //存到state里面
          // that.global.global.openCity = cityList[i];
        }
      }
      that.cityList = cityList;
      that.$router.go(-1);
    }
  },
  mounted() {
    var that = this;
    console.log(this.openCity);
    network.get(configUrl.OPENCITY_URL,'',function (res) {
      console.log(res);
      var cityList = res.dataList
      for (var i in cityList) {
        cityList[i].isChoose = false;
        if (cityList[i].areaId == that.openCity.areaId) {
          cityList[i].isChoose = true;
          that.cityIdx = i;
        }
      }
      if (that.openCity=='' || that.openCity==undefined) {
        cityList[0].isChoose = true;
      }

      that.cityList = cityList;
    },function (error) {
      console.log(error);
    });
    that.$nextTick(()=>{
      console.log(that.scrollTop);
      window.onScroll = function () {
        console.log('滚动');
      }
    })

  },
  updated () {
    this.$nextTick(function () {
      // Code that will run only after the
      // entire view has been re-rendered
      let elemId = document.getElementsByTagName('li');   //获取对应id的元素
      this.scrollTop = elemId[this.cityIdx].offsetTop;
      console.log(this.scrollTop);
      // window.addEventListener('scroll', function() {    //监听页面滚动
      //   console.log('滚动');
      // })
      document.body.scrollTop = this.scrollTop;
    })
  }
}
